<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}菜单`">
    <el-form
      ref="ruleFormRef"
      label-width="150px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <div class="men-tit">通用配置:</div>
      <el-form-item label="父菜单" prop="parentId" v-show="drawerProps.row.parentId">
        <el-input disabled v-model="drawerProps.row.parentId" placeholder="父菜单ID" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜单名称" prop="name">
        <el-input v-model="drawerProps.row.name" placeholder="请填写菜单名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜单名称（英文）" prop="nameEn">
        <el-input v-model="drawerProps.row.nameEn" placeholder="请填写菜单名称（英文）" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜单URL" prop="url">
        <el-input v-model="drawerProps.row.url" placeholder="请填写菜单URL" clearable></el-input>
      </el-form-item>
      <el-form-item label="组件URL" prop="component ">
        <el-input v-model="drawerProps.row.component" placeholder="请填写组件URL" clearable></el-input>
      </el-form-item>
      <el-form-item label="组件名称" prop="componentName">
        <el-input v-model="drawerProps.row.componentName" placeholder="请填写组件名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="重定向URL" prop="redirect">
        <el-input v-model="drawerProps.row.redirect" placeholder="请填写重定向URL" clearable></el-input>
      </el-form-item>
      <el-form-item label="高亮URL" prop="activeMenu">
        <el-input v-model="drawerProps.row.activeMenu" placeholder="请填写高亮URL" clearable></el-input>
      </el-form-item>
      <el-form-item label="授权标志符" prop="perms">
        <el-input v-model="drawerProps.row.perms" placeholder="请填写授权标志符" clearable></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="typeId">
        <el-select v-model="drawerProps.row.typeId" placeholder="请选择类型" clearable>
          <el-option v-for="item in drawerProps.menuList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="菜单图标" prop="icon">
        <el-select v-model="drawerProps.row.icon" placeholder="请选择图标" filterable clearable>
          <el-option v-for="item in icons" :key="item" :label="item" :value="item">
            <span style="float: left; font-size: 16px">
              <el-icon>
                <component :is="item"></component>
              </el-icon>
            </span>
            <span style="float: right">{{ item }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="外链地址" prop="isLink">
        <el-input v-model="drawerProps.row.isLink" placeholder="请填写外链地址" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否隐藏" prop="isHide">
        <el-switch v-model="drawerProps.row.isHide" :active-value="true" :inactive-value="false"></el-switch>
      </el-form-item>
      <el-form-item label="是否全屏" prop="isFull">
        <el-switch v-model="drawerProps.row.isFull" :active-value="true" :inactive-value="false"></el-switch>
      </el-form-item>
      <el-form-item label="是否固定标签" prop="isAffix">
        <el-switch v-model="drawerProps.row.isAffix" :active-value="true" :inactive-value="false"></el-switch>
      </el-form-item>
      <el-form-item label="是否缓存" prop="isKeepAlive">
        <el-switch v-model="drawerProps.row.isKeepAlive" :active-value="true" :inactive-value="false"></el-switch>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="drawerProps.row.sort" placeholder="请填写排序" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="drawerProps.row.remark" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
      <el-divider />
      <div class="men-tit">表格配置:</div>
      <el-form-item label="是否筛选" prop="isFilter">
        <el-switch v-model="drawerProps.row.isFilter" :active-value="true" :inactive-value="false"></el-switch>
      </el-form-item>
      <el-form-item label="是否排序" prop="isSort">
        <el-switch v-model="drawerProps.row.isSort" :active-value="true" :inactive-value="false"></el-switch>
      </el-form-item>
      <el-form-item label="字段名" prop="fieldName">
        <el-input v-model="drawerProps.row.fieldName" placeholder="请填写字段名" clearable></el-input>
      </el-form-item>

      <el-form-item label="字段值类型" prop="fieldValueType">
        <el-radio-group v-model="drawerProps.row.fieldValueType" @change="handleCheckedCitiesChange">
          <el-radio value="1">字典</el-radio>
          <el-radio value="2">接口</el-radio>
          <el-radio value="3">自定义</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="字段值" prop="fieldValue" v-if="drawerProps.row.fieldValueType != '3'">
        <template v-if="drawerProps.row.fieldValueType === '1'">
          <el-select
            v-model="drawerProps.row.fieldValue"
            placeholder="请选择"
            clearable
            filterable
            remote
            :remote-method="getSearchDictListFn"
            :loading="loading"
          >
            <el-option v-for="item in dictList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </template>
        <template v-else>
          <el-input v-model="drawerProps.row.fieldValue" placeholder="请填写接口名" clearable></el-input>
        </template>
      </el-form-item>

      <el-form-item label="字段组件类型" prop="fieldType">
        <el-input v-model="drawerProps.row.fieldType" placeholder="请填写字段组件类型" clearable></el-input>
      </el-form-item>
      <el-form-item label="关联表名" prop="tableName">
        <el-input v-model="drawerProps.row.tableName" placeholder="请填写字段关联表名" clearable></el-input>
      </el-form-item>
      <el-form-item label="字段操作符" prop="fieldOperate">
        <el-select v-model="drawerProps.row.fieldOperate">
          <el-option v-for="item in filterList" :key="item.value" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="资源类型" prop="resourceType">
        <el-input v-model="drawerProps.row.resourceType" placeholder="请填写资源类型" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="MenDrawer">
import { ref, reactive, getCurrentInstance, onMounted } from "vue";
// import { genderType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { getSearchDictList } from "@/api/modules/sys"; // getDict
import { Menu } from "@/api/interface";
import { useDict } from "@/hooks/useDict";

//import UploadImg from "@/components/Upload/Img.vue";
// import UploadImgs from "@/components/Upload/Imgs.vue";
const cxt = getCurrentInstance();
const { getDictAsync } = useDict();
const icons = cxt?.appContext.config.globalProperties.$icons;
const rules = reactive({
  name: [{ required: true, message: "请填写名称" }],
  url: [{ required: true, message: "请填写菜单URL" }],
  component: [{ required: true, message: "请填写组件URL" }],
  componentName: [{ required: true, message: "请填写组件名称" }],
  typeId: [{ required: true, message: "请选择类型" }],
  sort: [{ pattern: /^[0-9]+$/, message: "只能填写数字" }]
});
const dictList = ref<any>([]);
const loading = ref(false);
const filterList = ref<any>([]);

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Menu.ResMenuList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  type?: number;
  menuList?: Array<T>;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {} as Partial<Menu.ResMenuList>,
  type: 0,
  menuList: []
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      let params = JSON.parse(JSON.stringify(drawerProps.value.row));
      if (params.children) {
        delete params.children;
      }
      await drawerProps.value.api!(params);
      ElMessage.success({ message: `${drawerProps.value.title}菜单成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

const handleCheckedCitiesChange = (value: any) => {
  console.log(value);
};
const getSearchDictListFn = async (query: string) => {
  loading.value = true;
  const { data } = await getSearchDictList({ searchKey: query } as any);
  loading.value = false;
  dictList.value = data;
};
const init = async () => {
  const { data } = await getDictAsync("filterList");
  filterList.value = data ?? [];
};

onMounted(() => {
  init();
});

defineExpose({
  acceptParams
});
</script>
<style lang="scss" scoped>
.men-tit {
  font-size: 16px;
  margin-bottom: 10px;
  color: #333;
}
</style>
